#include <bits/stdc++.h>
using namespace std;

#define id(i, j) ((i) * (m + 3) + (j))

int a[200005];
int f[5000005][2];

int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int t;
    scanf("%d", &t);
    while (t--) {
        int n, m;
        scanf("%d", &n);
        if (n <= 2003) {
            m = 2003;
        } else {
            m = 13;
        }
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                f[id(i, j)][0] = f[id(i, j)][1] = -0x3f3f3f3f;
            }
        }
        f[id(1, 0)][0] = f[id(1, 0)][1] = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= m; j++) {
                f[id(i + 1, j)][0] = max(f[id(i + 1, j)][0], f[id(i, j)][0] + (a[i + 1] == a[i] ? a[i] : 0));
                f[id(i + 1, a[i])][0] = max(f[id(i + 1, a[i])][0], f[id(i, j)][1] + (a[i + 1] == j ? j : 0));
                f[id(i + 1, j)][1] = max(f[id(i + 1, j)][1], f[id(i, j)][1] + (a[i + 1] == a[i] ? a[i] : 0));
                f[id(i + 1, a[i])][1] = max(f[id(i + 1, a[i])][1], f[id(i, j)][0] + (a[i + 1] == j ? j : 0));
            }
        }
        int ans = 0;
        for (int i = 0; i <= m; i++) {
            ans = max(ans, f[id(n, i)][0]);
            ans = max(ans, f[id(n, i)][1]);
        }
        printf("%d\n", ans);
    }
}
